package com.carcenter.mq.listener;


import com.carcenter.common.pojo.order.AppointmentMessage;
import com.carcenter.common.result.Result;
import com.carcenter.common.result.enums.impl.ResultCode;
import com.order.client.OrderApi;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 - 监听超时未支付的订单
 */
@Component
@RabbitListener(queues = "appointment.dlx.queue")
@Slf4j
public class AppointmentCancelListener {
    @Autowired
    OrderApi orderApi;

    @SneakyThrows
    @RabbitHandler
    public void handle(@Payload AppointmentMessage message, Channel channel,
                       @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag) {
        log.info("监听到超时未支付的预约号：{}", message.getAppointmentId());
        Result<String> result = orderApi.invalidAppointment(message.getAppointmentId(), message.getUserId());
        if (result!=null && result.getCode()== ResultCode.SUCCESS.getCode()) {
            log.info("订单超时未支付，已取消，预约号：{}", message.getAppointmentId());

        }else {
            log.info("订单超时未支付，已取消操作，远程调用失败，预约号：{}", message.getAppointmentId());
        }
        //手动确认消息
        channel.basicAck(deliveryTag, false);
    }
}
